###############################################
### Project: Communicating in an eventful   ###
###          campaign                       ###
### Task:    Preparation - Manifestos       ###
### Title:   03_Preparation_Manifestos.R    ###
###############################################

#---------------------------------------------------------------------------------
# Description:
#
# This script downloads manifesto data from the Manifesto Project corpus for the
# German federal election campaign in 2021 and prepares the data for further use
# during the analysis.
#
# Attention: To run the script users need to provide an API key for downloading
# the manifestos!
#---------------------------------------------------------------------------------

#---------------------------------------------------------------------------------------------------------------------

# Preparation

## load packages
library(tidyverse)
library(xlsx)
library(manifestoR)

#---------------------------------------------------------------------------------------------------------------------

# Download and filter manifestos

## download manifestos (Germany 2021)
mp_setapikey(key.file = NULL, key = "") # provide API key

election_programmes <- mp_corpus(countryname == "Germany" & date == "202109")

corpus <- c()
for(i in 1:length(election_programmes)) {
  
      res_tmp <- cbind(id = election_programmes[[i]][[2]]$manifesto_id,
                       party_id = election_programmes[[i]][[2]]$party,
                       date = election_programmes[[i]][[2]]$date,
                       annotation = election_programmes[[i]][[2]]$annotations,
                       election_programmes[[i]][[1]])
      
      corpus <- rbind(corpus, res_tmp)
}

## filter data set
data <- filter(corpus, !is.na(cmp_code) & cmp_code != "H")

## add party names
df_partynames <- data.frame(party_id = c(41113, 41223, 41320, 41420, 41521, 41953),
                            party  = c("Grüne", "Linke", "SPD", "FDP", "CDU/CSU", "AfD"))

data <- left_join(data, df_partynames, by = "party_id")

data <- data[, c(1:2, 8, 3:7)]

#---------------------------------------------------------------------------------------------------------------------

# Merge manifesto data codes with issues from codebook

## load codebook
codebook <- read.xlsx("Codebook.xlsx", sheetIndex = 1)
codes <- codebook[,5:6]
colnames(codes) <- c("issue", "cmp_code")
codes$issue <- gsub(" ", "_", codes$issue)

## merge
data <- left_join(data, codes, by="cmp_code")
data$issue <- gsub("_", " ", data$issue)

#---------------------------------------------------------------------------------------------------------------------

# Save final data set

saveRDS(data, "Corpus_Manifestos_DE_2021.RDS")
